package com.xc.xcmanage.web.controller;

import com.xc.xcmanage.domain.AjaxResult;
import com.xc.xcmanage.domain.dto.LoginBody;
import com.xc.xcmanage.domain.pojo.SysMenu;
import com.xc.xcmanage.service.SysMenuService;
import com.xc.xcmanage.service.SysUserService;
import com.xc.xcmanage.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "登录相关接口")
@RestController
public class LoginController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysMenuService menuService;

    @ApiOperation("登录")
    @PostMapping("/user/login")
    public Object login(@ApiParam("登录参数") @RequestBody LoginBody loginBody){

        return sysUserService.login(loginBody);
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @ApiOperation("获取路由")
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        // 从springsecurity中获取当前登录的用户
        Long userId = SecurityUtils.getUserId();
        // 用户能看哪些菜单
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);

        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
